package edu.strategy.single.scanned;

import robocode.AdvancedRobot;
import robocode.Event;
import robocode.ScannedRobotEvent;
import edu.strategy.single.Single;

public class ProjectMarsScannedStrategy extends Single{	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ProjectMarsScannedStrategy(AdvancedRobot advancedRobot) {
		super(advancedRobot);
	}
	
	@Override
	public void prepare() {}

	@Override
	public void work() {
		super.getAdvancedRobot().fire(1);
	}

	@Override
	public void work(Event e) {
		super.used();
		ScannedRobotEvent srv = (ScannedRobotEvent) e;
		
		// Calculate exact location of the robot
		double absoluteBearing = super.getAdvancedRobot().getHeading() + srv.getBearing();
		double bearingFromGun = normalRelativeAngle(absoluteBearing - super.getAdvancedRobot().getGunHeading());
		// If it's close enough, fire!
			if (Math.abs(bearingFromGun) <= 3) {
				super.getAdvancedRobot().setTurnGunRight(bearingFromGun);
				// We check gun heat here, because calling fire() 
				// uses a turn, which could cause us to lose track
				// of the other robot.
				if (super.getAdvancedRobot().getGunHeat() == 0)
					if(srv.getDistance() < 30) {
						super.getAdvancedRobot().setFire(4);
					}
					if(srv.getDistance() > 29 && srv.getDistance() < 60) {
						super.getAdvancedRobot().setFire(3.6);
					}
					if(srv.getDistance() > 59 && srv.getDistance() < 90) {
						super.getAdvancedRobot().setFire(3.2);
					}
					if(srv.getDistance() > 89 && srv.getDistance() < 120) {
						super.getAdvancedRobot().setFire(2.8);
					}
					if(srv.getDistance() > 119 && srv.getDistance() < 150) {
						super.getAdvancedRobot().setFire(2.4);
					}
					if(srv.getDistance() > 149 && srv.getDistance() < 180) {
						super.getAdvancedRobot().setFire(2);
					}
					if(srv.getDistance() > 179 && srv.getDistance() < 210) {
						super.getAdvancedRobot().setFire(1.6);
					}
					if(srv.getDistance() > 209 && srv.getDistance() < 240) {
						super.getAdvancedRobot().setFire(1.2);
					}
					if(srv.getDistance() > 239 && srv.getDistance() < 270) {
						super.getAdvancedRobot().setFire(.8);
					}
					if(srv.getDistance() > 269 && srv.getDistance() < 300) {
						super.getAdvancedRobot().setFire(.4);
					}
					if(srv.getDistance() > 299) {
						super.getAdvancedRobot().setFire(.3);
					}
				}
		else {
			super.getAdvancedRobot().setTurnGunRight(bearingFromGun);
		}
		// Generates another scan event if we see a robot.
		// We only need to call this if the gun (and therefore radar)
		// are not turning.  Otherwise, scan is called automatically.
		if (bearingFromGun == 0){
			super.getAdvancedRobot().scan();
		}
		super.getAdvancedRobot().execute();
	}

	public double normalRelativeAngle(double angle) {
		if (angle > -180 && angle <= 180)
			return angle;
		double fixedAngle = angle;
		while (fixedAngle <= -180)
			fixedAngle += 360;
		while (fixedAngle > 180)
			fixedAngle -= 360;
		return fixedAngle;
	}

	@Override
	public String toString() {
		return "ProjectMarsScannedStrategy";
	}
}
